package vip.buddha.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import vip.buddha.entity.Orders;
import vip.buddha.feign.AccountService;
import vip.buddha.feign.StorageService;
import vip.buddha.handler.CustomerBlockHandler;
import vip.buddha.handler.CustomerFallBack;
import vip.buddha.service.OrdersService;

import javax.annotation.Resource;

@RestController
@Slf4j
public class OrderController {
    @Resource
    private AccountService accountService;

    @Resource
    private StorageService storageService;

    @Autowired
    private OrdersService ordersService;

    @SentinelResource(value = "list",
            blockHandlerClass = CustomerBlockHandler.class,
            blockHandler = "down_1",
            fallbackClass = CustomerFallBack.class,
            fallback = "fallback_1")
    @RequestMapping("/order/list")
    public String list(@RequestParam(name = "type", required = false) String type,
                       @RequestParam(name = "name", required = false) String name) {
        String s1 = accountService.list();
        String s2 = storageService.list();
        System.out.println(s1 + "," + s2);
        // 模拟报错
        //System.out.println(1 / 0);

        return "this is order list api" + type + " " + name;
    }

    @PostMapping("/order")
    public String save(@RequestBody Orders orders) throws Exception {
        int i = ordersService.save(orders);
        log.info(orders.toString());
        log.info("-----------------");
        log.info(" " + i);
        return "ok";
    }
}
